package com.meihua.shellapp.activity.maker;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aspsine.irecyclerview.IRecyclerView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.adapter.personalcenter.MakerSelectAdapter;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.MakerBean;
import com.meihua.shellapp.databean.MakerDataBean;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;
import com.meihua.shellapp.view.footer.LoadMoreFooterView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：lc on 2017/9/28.
 * 邮箱：lc0607live@163.com
 */

public class MakerSelectActivity extends BaseActivity {

    @Bind(R.id.iv_top_left)
    ImageView iv_top_left;//返回
    @Bind(R.id.tv_title)
    TextView tv_title;//标题
    @Bind(R.id.rv)
    IRecyclerView rv;//列表

    private MakerSelectAdapter mAdapter;
    private List<MakerDataBean> data = new ArrayList<>();
    private int mPage = 1;
    private LoadMoreFooterView loadMoreFooterView;

    @Override
    public int getLayoutId() {
        return R.layout.activity_layout_rv;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        iv_top_left.setVisibility(View.VISIBLE);
        iv_top_left.setOnClickListener(this);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("创客查询");
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rv.setLayoutManager(layoutManager);
        loadMoreFooterView = (LoadMoreFooterView) rv.getLoadMoreFooterView();
        mAdapter = new MakerSelectAdapter(mContext, data);
        rv.setIAdapter(mAdapter);
        rv.setOnLoadMoreListener(this);
        rv.setOnRefreshListener(this);
    }

    @Override
    public void createLoadData() {
        super.createLoadData();
        getData();
    }


    private void getData() {
        OkGo.<String>post(URL.getUrl().SELECTMAKER)
                .params("m_number", MyApplication.getInstance().getUserId())
                .params("page", mPage)
                .execute(new MyStringCallBack<MakerBean>(rv) {

                    @Override
                    public void onSuccessData(MakerBean bean) {
                        super.onSuccessData(bean);
                        if (bean.getData() != null && bean.getData().size() > 0) {
                            if (mPage == 1) {
                                data.clear();
                            }
                            data.addAll(bean.getData());
                            mAdapter.notifyDataSetChanged();
                        } else {
                            if (mPage == 1) {
                                data.clear();
                                mAdapter.notifyDataSetChanged();
                            }
                            endRefreshing(rv);
                            ToastUtils.getInstance().showToast(bean.getMsg());
                        }
                    }

                });
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        mPage = 1;
        getData();
    }

    @Override
    public void onLoadMore() {
        super.onLoadMore();
        if (loadMoreFooterView.canLoadMore() && mAdapter.getItemCount() > 0) {
            loadMoreFooterView.setStatus(LoadMoreFooterView.Status.LOADING);
            mPage++;
            getData();
        }
    }
}
